package com.sky.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sky.blog.dao.enums.StatusCode;
import com.sky.blog.dao.mapper.SocialMapper;
import com.sky.blog.dao.pojo.Social;
import com.sky.blog.dao.vo.BlogSocialVo;
import com.sky.blog.service.SocialService;
import com.sky.blog.util.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sky  2022/3/27
 * @version 1.0
 */

@Slf4j
@Service
public class SocialServiceImpl implements SocialService {

    @Resource
    private SocialMapper socialMapper;

    /**
     * 获取所有的博主的第三方信息
     * <ol>
     *     <li>初始化查询条件</li>
     *     <li><span>查询条件</span>
     *      <ul>
     *          <li>按热度升序</li>
     *          <li>状态正常</li>
     *
     *      </ul>
     *     </li>
     *     <li>将查询结果封装到{@link BlogSocialVo}</li>
     * </ol>
     *
     * @return social
     */
    @Override
    public List<BlogSocialVo> getSocial() {

        List<Social> socialList = new ArrayList<>();
        LambdaQueryWrapper<Social> wrapper = Wrappers.lambdaQuery(Social.class);
        wrapper.eq(Social::getStatus, StatusCode.NORMAL);
        wrapper.orderByAsc(Social::getSort);

        try {
            socialList.addAll(socialMapper.selectList(wrapper));
        } catch (Exception e) {
            log.error("查询失败,错误信息:{}", e.getMessage());
        }

        return socialList.stream()
                .map(social -> DozerUtils.copy(social, BlogSocialVo.class))
                .collect(Collectors.toList());
    }
}
